﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;

using IncubusCC.Settings;
using Zeta.CommonBot.Settings;
using IncubusCC.Helpers;

namespace IncubusCC.GUI
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : UserControl
    {
        public SettingsWindow()
        {
            DisplayWindow();
        }

        private static string _Path = Path.Combine(Path.Combine(GlobalSettings.Instance.CombatRoutinePath, "IncubusCC", "GUI"), "SettingsWindow.xaml");
        private static Window configWindow = null;
        private static ScrollViewer Scroller;
        private static TextBox cHealthPotion, bFCDreadHP, bIgnorePain, bRageAoE, bOverpowerAoE, bWhirlwindAoe, dhPreperation, dhShadowHP, dhSmokeHP, bLeapDistance, bFuriousChargeDistance;
        private static TextBox cHealthGlobeHP, cHealthGlobeDistance, mBreathOfHeavenHp, mSerenityHP, wdSacrifice, wArchon, wDiamondSkin, wMirrorImage, wSlowTime, dhVaultDistance, wTeleportDistance;
        private static CheckBox cMoveToHealthGlobe, bSpamWarCry, bLeapOCD, bFCDread, dhVault, dhSpamSS, mSpamMantra, mBoHBlazingWrath, wTeleport;
        private static Button saveBtn;

        public static Window DisplayWindow()
        {
            if (!File.Exists(_Path))
                Logger.WriteVerbose("ERROR: Can't find \"" + _Path);
            try
            {
                if (configWindow == null)
                {
                    configWindow = new Window();
                }
                StreamReader xamlStream = new StreamReader(_Path);
                DependencyObject xamlContent = System.Windows.Markup.XamlReader.Load(xamlStream.BaseStream) as DependencyObject;
                configWindow.Content = xamlContent;

                UserControl mainControl = LogicalTreeHelper.FindLogicalNode(xamlContent, "mainControl") as UserControl;
                // Set height and width to main window
                configWindow.Height = mainControl.Height + 30;
                configWindow.Width = mainControl.Width + 20;
                configWindow.Title = "IncubusCC Settings";


                Scroller = LogicalTreeHelper.FindLogicalNode(xamlContent, "Scroller") as ScrollViewer;

                //Common Settings
                cHealthPotion = LogicalTreeHelper.FindLogicalNode(xamlContent, "HealthPotionBox") as TextBox;
                cMoveToHealthGlobe = LogicalTreeHelper.FindLogicalNode(xamlContent, "HGEnable") as CheckBox;
                cHealthGlobeDistance = LogicalTreeHelper.FindLogicalNode(xamlContent, "HealthGlobeDistance") as TextBox;
                cHealthGlobeHP = LogicalTreeHelper.FindLogicalNode(xamlContent, "HealthGlobeHp") as TextBox;

                //Barbarian
                bSpamWarCry = LogicalTreeHelper.FindLogicalNode(xamlContent, "SpamWarCry") as CheckBox;
                bFCDread = LogicalTreeHelper.FindLogicalNode(xamlContent, "FCDread") as CheckBox;
                bFCDreadHP = LogicalTreeHelper.FindLogicalNode(xamlContent, "FCDreadHP") as TextBox;
                bLeapOCD = LogicalTreeHelper.FindLogicalNode(xamlContent, "LeapOCD") as CheckBox;
                bIgnorePain = LogicalTreeHelper.FindLogicalNode(xamlContent, "IgnorePain") as TextBox;
                bRageAoE = LogicalTreeHelper.FindLogicalNode(xamlContent, "RageAoE") as TextBox;
                bOverpowerAoE = LogicalTreeHelper.FindLogicalNode(xamlContent, "OverpowerAoE") as TextBox;
                bWhirlwindAoe = LogicalTreeHelper.FindLogicalNode(xamlContent, "WhirlwindAoe") as TextBox;
                bLeapDistance = LogicalTreeHelper.FindLogicalNode(xamlContent, "LeapDistance") as TextBox;
                bFuriousChargeDistance = LogicalTreeHelper.FindLogicalNode(xamlContent, "FuriousChargeDistance") as TextBox;

                //Demon Hunter
                dhPreperation = LogicalTreeHelper.FindLogicalNode(xamlContent, "PreparationHP") as TextBox;
                dhShadowHP = LogicalTreeHelper.FindLogicalNode(xamlContent, "ShadowPowerHP") as TextBox;
                dhSmokeHP = LogicalTreeHelper.FindLogicalNode(xamlContent, "SmokeScreenHP") as TextBox;
                dhSpamSS = LogicalTreeHelper.FindLogicalNode(xamlContent, "SpamSS") as CheckBox;
                dhVault = LogicalTreeHelper.FindLogicalNode(xamlContent, "Vault") as CheckBox;
                dhVaultDistance = LogicalTreeHelper.FindLogicalNode(xamlContent, "VaultDistance") as TextBox;

                //Monk
                mBreathOfHeavenHp = LogicalTreeHelper.FindLogicalNode(xamlContent, "BreathOfHeavenHp") as TextBox;
                mSerenityHP = LogicalTreeHelper.FindLogicalNode(xamlContent, "SerenityHP") as TextBox;
                mBoHBlazingWrath = LogicalTreeHelper.FindLogicalNode(xamlContent, "BoHBlazingWrath") as CheckBox;
                mSpamMantra = LogicalTreeHelper.FindLogicalNode(xamlContent, "SpamMantra") as CheckBox;

                //Witch Doctor
                wdSacrifice = LogicalTreeHelper.FindLogicalNode(xamlContent, "SacrificeHp") as TextBox;

                //Wizzard
                wArchon = LogicalTreeHelper.FindLogicalNode(xamlContent, "ArchonAoECount") as TextBox;
                wDiamondSkin = LogicalTreeHelper.FindLogicalNode(xamlContent, "DiamondSkinHp") as TextBox;
                wMirrorImage = LogicalTreeHelper.FindLogicalNode(xamlContent, "MirrorImageHp") as TextBox;
                wSlowTime = LogicalTreeHelper.FindLogicalNode(xamlContent, "SlowTimeHp") as TextBox;
                wTeleport = LogicalTreeHelper.FindLogicalNode(xamlContent, "Teleport") as CheckBox;
                wTeleportDistance = LogicalTreeHelper.FindLogicalNode(xamlContent, "TeleportDistance") as TextBox;

                //Save Button
                saveBtn = LogicalTreeHelper.FindLogicalNode(xamlContent, "SaveBTN") as Button;
                saveBtn.Click += new RoutedEventHandler(saveBtn_Click);

                // On load example
                configWindow.Loaded += new RoutedEventHandler(configWindow_Loaded);
                configWindow.Closed += configWindow_Closed;

                // Add our content to our main window
                configWindow.Content = xamlContent;

            }

            catch (System.Windows.Markup.XamlParseException ex)
            {
                // You can get specific error information like LineNumber from the exception
                Logger.WriteVerbose(ex.ToString());
            }
            catch (Exception ex)
            {
                // Some other error
                Logger.WriteVerbose(ex.ToString());
            }
            return configWindow;
        }

        private static void configWindow_Closed(object sender, EventArgs e)
        {
            configWindow = null;
        }

        private static void Save()
        {
            try
            {
                IncubusCCSettings.Instance.HealthPotionPct = Double.Parse(cHealthPotion.Text);
                IncubusCCSettings.Instance.GetHealthGlobe = cMoveToHealthGlobe.IsChecked ?? false;
                IncubusCCSettings.Instance.HealthGlobeHP = double.Parse(cHealthGlobeHP.Text);
                IncubusCCSettings.Instance.HealthGlobeDistance = float.Parse(cHealthGlobeDistance.Text);

                IncubusCCSettings.Instance.Barbarain.SpamWarCry = bSpamWarCry.IsChecked ?? false;
                IncubusCCSettings.Instance.Barbarain.FCDreadnought = bFCDread.IsChecked ?? false;
                IncubusCCSettings.Instance.Barbarain.FCDreadnoughtHP = double.Parse(bFCDreadHP.Text);
                IncubusCCSettings.Instance.Barbarain.LeapOCD = bLeapOCD.IsChecked ?? false;
                IncubusCCSettings.Instance.Barbarain.IgnorePainPct = Double.Parse(bIgnorePain.Text);
                IncubusCCSettings.Instance.Barbarain.RageSkillsAoeCount = int.Parse(bRageAoE.Text);
                IncubusCCSettings.Instance.Barbarain.OverpowerAoeCount = int.Parse(bOverpowerAoE.Text);
                IncubusCCSettings.Instance.Barbarain.WhirlwindAoeCount = int.Parse(bWhirlwindAoe.Text);
                IncubusCCSettings.Instance.Barbarain.LeapDistance = float.Parse(bLeapDistance.Text);
                IncubusCCSettings.Instance.Barbarain.FuriosChargeDistance = float.Parse(bFuriousChargeDistance.Text);

                IncubusCCSettings.Instance.DemonHunter.PrperationHP = Double.Parse(dhPreperation.Text);
                IncubusCCSettings.Instance.DemonHunter.ShadowPowerHp = Double.Parse(dhShadowHP.Text);
                IncubusCCSettings.Instance.DemonHunter.SmokeScreenHP = Double.Parse(dhSmokeHP.Text);
                IncubusCCSettings.Instance.DemonHunter.SpamSmokeScreen = dhSpamSS.IsChecked ?? false;
                IncubusCCSettings.Instance.DemonHunter.Vault = dhVault.IsChecked ?? false;
                IncubusCCSettings.Instance.DemonHunter.VaultDistance = float.Parse(dhVaultDistance.Text);

                IncubusCCSettings.Instance.Monk.BoHBlazingWrath = mBoHBlazingWrath.IsChecked ?? false;
                IncubusCCSettings.Instance.Monk.BreathOfHeavenHp = double.Parse(mBreathOfHeavenHp.Text);
                IncubusCCSettings.Instance.Monk.SerenityHp = double.Parse(mSerenityHP.Text);
                IncubusCCSettings.Instance.Monk.SpamMantra = mSpamMantra.IsChecked ?? false;

                IncubusCCSettings.Instance.WitchDoctor.SacrificeHp = double.Parse(wdSacrifice.Text);

                IncubusCCSettings.Instance.Wizard.ArchonAoECount = int.Parse(wArchon.Text);
                IncubusCCSettings.Instance.Wizard.DiamondSkinHp = double.Parse(wDiamondSkin.Text);
                IncubusCCSettings.Instance.Wizard.MirrorImageHp = double.Parse(wMirrorImage.Text);
                IncubusCCSettings.Instance.Wizard.SlowTimeHp = double.Parse(wSlowTime.Text);
                IncubusCCSettings.Instance.Wizard.Teleport = wTeleport.IsChecked ?? false;
                IncubusCCSettings.Instance.Wizard.TeleportDistance = float.Parse(wTeleportDistance.Text);

                IncubusCCSettings.Instance.Save();
                IncubusCCSettings.Instance.Barbarain.Save();
                IncubusCCSettings.Instance.DemonHunter.Save();
                IncubusCCSettings.Instance.Monk.Save();
                IncubusCCSettings.Instance.WitchDoctor.Save();
                IncubusCCSettings.Instance.Wizard.Save();

                Logger.WriteLog("Settings Saved");
            }
            catch (FormatException fm)
            {
                Logger.WriteLog("You entered something wrong, your settings havent been changed");
                Logger.WriteVerbose(fm.ToString());
            }
            catch (Exception ex)
            {
                Logger.WriteVerbose(ex.ToString());
            }
        }

        private static void configWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Load Common
            cHealthPotion.Text = IncubusCCSettings.Instance.HealthPotionPct.ToString();
            cHealthGlobeHP.Text = IncubusCCSettings.Instance.HealthGlobeHP.ToString();
            cHealthGlobeDistance.Text = IncubusCCSettings.Instance.HealthGlobeDistance.ToString();
            if (IncubusCCSettings.Instance.GetHealthGlobe)
                cMoveToHealthGlobe.IsChecked = true;
            else cMoveToHealthGlobe.IsChecked = false;

            //Load Barbarian
            if (IncubusCCSettings.Instance.Barbarain.SpamWarCry)
                bSpamWarCry.IsChecked = true;
            else bSpamWarCry.IsChecked = false;

            if (IncubusCCSettings.Instance.Barbarain.FCDreadnought)
                bFCDread.IsChecked = true;
            else bFCDread.IsChecked = false;

            if (IncubusCCSettings.Instance.Barbarain.LeapOCD)
                bLeapOCD.IsChecked = true;
            else bLeapOCD.IsChecked = false;

            bFCDreadHP.Text = IncubusCCSettings.Instance.Barbarain.FCDreadnoughtHP.ToString();
            bIgnorePain.Text = IncubusCCSettings.Instance.Barbarain.IgnorePainPct.ToString();
            bRageAoE.Text = IncubusCCSettings.Instance.Barbarain.RageSkillsAoeCount.ToString();
            bOverpowerAoE.Text = IncubusCCSettings.Instance.Barbarain.OverpowerAoeCount.ToString();
            bWhirlwindAoe.Text = IncubusCCSettings.Instance.Barbarain.WhirlwindAoeCount.ToString();
            bLeapDistance.Text = IncubusCCSettings.Instance.Barbarain.LeapDistance.ToString();
            bFuriousChargeDistance.Text = IncubusCCSettings.Instance.Barbarain.FuriosChargeDistance.ToString();

            //Load Demon Hunter
            dhPreperation.Text = IncubusCCSettings.Instance.DemonHunter.PrperationHP.ToString();
            dhShadowHP.Text = IncubusCCSettings.Instance.DemonHunter.ShadowPowerHp.ToString();
            dhSmokeHP.Text = IncubusCCSettings.Instance.DemonHunter.SmokeScreenHP.ToString();
            dhVaultDistance.Text = IncubusCCSettings.Instance.DemonHunter.VaultDistance.ToString();
            

            if (IncubusCCSettings.Instance.DemonHunter.SpamSmokeScreen)
                dhSpamSS.IsChecked = true;
            else dhSpamSS.IsChecked = false;

            if (IncubusCCSettings.Instance.DemonHunter.Vault)
                dhVault.IsChecked = true;
            else dhVault.IsChecked = false;

            //Load Monk
            mBreathOfHeavenHp.Text = IncubusCCSettings.Instance.Monk.BreathOfHeavenHp.ToString();
            mSerenityHP.Text = IncubusCCSettings.Instance.Monk.SerenityHp.ToString();

            if (IncubusCCSettings.Instance.Monk.BoHBlazingWrath)
                mBoHBlazingWrath.IsChecked = true;
            else mBoHBlazingWrath.IsChecked = false;

            if (IncubusCCSettings.Instance.Monk.SpamMantra)
                mSpamMantra.IsChecked = true;
            else mSpamMantra.IsChecked = false;

            //Load WD
            wdSacrifice.Text = IncubusCCSettings.Instance.WitchDoctor.SacrificeHp.ToString();

            //Load Wizard
            wArchon.Text = IncubusCCSettings.Instance.Wizard.ArchonAoECount.ToString();
            wDiamondSkin.Text = IncubusCCSettings.Instance.Wizard.DiamondSkinHp.ToString();
            wMirrorImage.Text = IncubusCCSettings.Instance.Wizard.MirrorImageHp.ToString();
            wSlowTime.Text = IncubusCCSettings.Instance.Wizard.SlowTimeHp.ToString();
            wTeleportDistance.Text = IncubusCCSettings.Instance.Wizard.TeleportDistance.ToString();

            if (IncubusCCSettings.Instance.Wizard.Teleport)
                wTeleport.IsChecked = true;
            else wTeleport.IsChecked = false;

        }

        private static void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }
    }
}
